package com.moyu.controller;

import com.moyu.bean.Msg;
import com.moyu.bean.Student;
import com.moyu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping(value = "/student")
public class StudentController {

    @Autowired
    StudentService studentService;


    /**
     * @author moyu
     * @info 获取所有学生
     * @date 18-2-28 下午4:04
     * @param
     * @return com.moyu.bean.Msg
     */
    @RequestMapping(value = "/query/all",method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllStudent(){

        Msg msg=new Msg();
        msg.setMsg("查询全部外出信息成功");
        msg.setStatus(100);
        return msg.add("allStudentInfo",studentService.getAll());
    }

    /**
     * @author moyu
     * @info 根据学号查询学生信息
     * @author chenmc
     * @date 2018/2/28 18:17
     * @param studentUsername
     * @return com.moyu.bean.Msg
     */
    @RequestMapping(value = "/query/{studentUsername}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getStudentByUsername(@PathVariable("studentUsername") String studentUsername){
        return Msg.success().add("studentInfo",studentService.getStudentByUsername(studentUsername));
    }

    @GetMapping("query/all/StudentUsername")
    @ResponseBody
    public Msg selectStudentPrimaryKey(){
        return Msg.success().add("studentUsername",studentService.selectStudentUsernames());
    }

    /**
     * @author moyu
     * @info 学生保存(可进行校验)
     * @author chenmc
     * @date 2018/2/28 17:57
     * @param student, result
     * @return com.moyu.bean.Msg
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public Msg saveStudent(@Valid Student student, BindingResult result){
        if (result.hasErrors()) {

            Map<String, Object> map = new HashMap<String, Object>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError : errors) {
                System.out.println("错误字段名"+fieldError.getField());
                System.out.println("错误信息"+fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            System.out.println("进入函数");
            return Msg.fail().add("errorFields", map);
        }else {



            studentService.saveStudent(student);
            return Msg.success();
        }
    }

    /**
     * @author moyu
     * @info 删除学生信息
     * @author chenmc
     * @date 2018/2/28 18:05
     * @param studentUsername
     * @return com.moyu.bean.Msg
     */
    @RequestMapping(value = "/del/{studentUsername}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg delStudent(@PathVariable("studentUsername")String studentUsername){
        studentService.delStudent(studentUsername);
        return Msg.success();
    }

    /**
     * @author moyu
     * @info 学生信息更新
     * @author chenmc
     * @date 2018/3/4 16:23
     * @param student
     * @return com.moyu.bean.Msg
     */
    @ResponseBody
    @RequestMapping(value="/update/{studentUsername}",method = RequestMethod.POST)
    public Msg updateStudent(Student student,@PathVariable("studentUsername") String studentUsername){
        System.out.println("请求进入");
        student.setStudentUsername(studentUsername);
        System.out.println(student.toString());
        studentService.updateStudent(student);
        return Msg.success();
    }

}
